/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vastcm.stuhealth.client.report.ui;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

import com.jidesoft.grid.RowStripeTableStyleProvider;
import com.vastcm.stuhealth.client.AppContext;
import com.vastcm.stuhealth.client.framework.report.DataBaseType;
import com.vastcm.stuhealth.client.framework.report.IJasperReportService;
import com.vastcm.stuhealth.client.framework.report.RptParamInfo;
import com.vastcm.stuhealth.client.framework.report.ui.ReportBasePanel;
import com.vastcm.stuhealth.client.framework.ui.UIHandler;
import com.vastcm.stuhealth.client.utils.MyTableUtils;

/**
 * 个体评价报表
 * 
 * @author bob
 */
public class StudentAppraiseRptPanel extends ReportBasePanel {

	private IJasperReportService jasperReportService;

	public StudentAppraiseRptPanel() {
		initComponents();

		setFilterBasePanel(new StudentAppraiseFilterPanel());
		tblMain.setTableStyleProvider(new RowStripeTableStyleProvider());
	}

	@Override
	protected void initRptParamInfo() {
		super.initRptParamInfo();
		rptParamInfo = new RptParamInfo();
		rptParamInfo.setColumnNames(new String[] { "班级", "编号", "姓名", "性别", "年龄", "身高(cm)", "评价", "体重(kg)", "评价", "胸围(cm)", "评价", "肺活量(ml)", "评价",
				"脉搏", "评价", "收缩压(mmHg)", "评价", "舒张压(mmHg)", "评价", "左眼", "右眼", "评价", "左眼", "右眼", "评价", "左眼", "右眼", "弱视", "左眼", "右眼", "色觉", "听力左",
				"听力右", "牙齿", "牙周", "胸部", "脊柱", "四肢", "心", "肝", "脾", "肺", "蛔虫卵", "神经衰弱", "血红蛋白(g/L)", "评价", "眼病", "头部", "颈部", "皮肤", "淋巴结", "结核菌素试验",
				"谷丙转氨酶(参考\n值0-40 IU/L)", "总胆红素(参考\n值0-20 umol/L)", "直胆红素(参考\n值0-20 umol/L)", "肝功能", "表面抗原", "表面抗体", "结膜炎", "窝沟封闭", "牙龈", "男性外生殖器",
				"营养评价", "发育", "id隐藏" });
	}

	@Override
	protected void initTable(boolean isRebuild) {
		super.initTable(isRebuild);
		int colIndex = 5;
		MyTableUtils.addColumnGroup2Table(tblMain, "身高", colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "体重", colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "胸围", colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "肺活量", colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "脉搏", colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "收缩压", colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "舒张压", colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "视力", colIndex++, colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "近视力", colIndex++, colIndex++, colIndex++);
		MyTableUtils.addColumnGroup2Table(tblMain, "屈光不正", colIndex++, colIndex++);
		colIndex++;
		MyTableUtils.addColumnGroup2Table(tblMain, "沙眼", colIndex++, colIndex++);
		colIndex = 44;
		MyTableUtils.addColumnGroup2Table(tblMain, "血红蛋白", colIndex++, colIndex++);

		int size = rptParamInfo.getColumnNames().length;
		MyTableUtils.hiddenColumn(tblMain, size - 2);
		MyTableUtils.hiddenColumn(tblMain, size - 1);
	}

	@Override
	public void beforeQuery(ActionEvent evt) {
		super.beforeQuery(evt);
		//		System.out.println(getWholeSQL());
		Map<String, Object> filterParams = rptParamInfo.getFilterParam();
		String standardFlag = "Q";//默认使用全国标准，广东标准作为评价的版本
		//		standardFlag = (String) filterParams.get(StudentAppraiseFilterPanel.Param_Standard_Option);
		StringBuffer sql = new StringBuffer(4000);
		sql.append("select CLASS_NAME,BH,XM,XB,NL,SG,SGDJ").append(standardFlag).append(",TZ,TZDJ").append(standardFlag);
		sql.append(",XW,XWDJ").append(standardFlag).append(",FHL,FHLDJ").append(standardFlag);
		sql.append(",MB,MBPJ,SSY,SSYPJ,SZY,SZYPJ,LSL,RSL,SLPJ,LJSL,RJSL,case when ljsl<5.0 or rjsl<5.0 then '视力低下' when ljsl>=5.0 and rjsl>=5.0 then '正常' else '' end JSLPJ");
		processItemResultSQL(sql, "LQG");
		processItemResultSQL(sql, "RQG");
		processItemResultSQL(sql, "RS");
		processItemResultSQL(sql, "LSY");
		processItemResultSQL(sql, "RSY");
		processItemResultSQL(sql, "BS");
		processItemResultSQL(sql, "TL");
		processItemResultSQL(sql, "RTL").append(",case when qsbnum>0 then '龋齿' else '无龋齿' end YCPJ");
		processItemResultSQL(sql, "YZB");
		processItemResultSQL(sql, "XK");
		processItemResultSQL(sql, "JZ");
		processItemResultSQL(sql, "SZ");
		processItemResultSQL(sql, "XZ");
		processItemResultSQL(sql, "GP");
		processItemResultSQL(sql, "PEI");
		processItemResultSQL(sql, "FEI");
		processItemResultSQL(sql, "FHCL");
		sql.append(",SJSR,XHDB,PXPJ");
		processItemResultSQL(sql, "YB");
		processItemResultSQL(sql, "TB");
		processItemResultSQL(sql, "JB");
		processItemResultSQL(sql, "PF");
		processItemResultSQL(sql, "LBJ");
		sql.append(",JHJS,GBZAM,DHSA,DHSZ");
		processItemResultSQL(sql, "GG");
		processItemResultSQL(sql, "BMKY");
		processItemResultSQL(sql, "BMKT");
		processItemResultSQL(sql, "JMY");
		processItemResultSQL(sql, "WGFB");
		processItemResultSQL(sql, "YYB");
		processItemResultSQL(sql, "WSZQ");
		sql.append(",YYPJ,FYPJ,ID \nfrom Result");
		sql.append("\n where 1=1 ");
		LinkedHashMap<String, Object> hqlParam = new LinkedHashMap<String, Object>();
		rptParamInfo.setHqlParam(hqlParam);
		FilterCommonAction.processFilter4SchoolRange(filterParams, sql, hqlParam);
		FilterCommonAction.processFilter4SchoolTerm(filterParams, sql, hqlParam);
		FilterCommonAction.processFilter4QueryPair(filterParams, sql, hqlParam);
		sql.append("\n order by schoolBH,ClassBH,XM");
		rptParamInfo.setQuerySQL(sql.toString());
	}

	private StringBuffer processNotNullSQL(StringBuffer sql, String columnName) {
		//SQL空函数
		if (DataBaseType.Current == DataBaseType.Mysql_And_SqlServer)
			sql.append("\n, isnull(").append(columnName).append(",'') ").append(columnName);
		else
			sql.append("\n, ifnull(").append(columnName).append(",'') ").append(columnName);

		return sql;
	}

	private StringBuffer processItemResultSQL(StringBuffer sql, String columnName) {
		if (DataBaseType.Current == DataBaseType.Mysql_And_SqlServer) {
			sql.append("\n, isnull((select ItemResult from ItemResult where FieldMc = '");
			sql.append(columnName).append("' and ItemCode = ").append(columnName);
			sql.append("), isnull(").append(columnName).append(",'')) ").append(columnName);
		} else {
			sql.append("\n, ifnull((select ItemResult from ItemResult where FieldMc = '");
			sql.append(columnName).append("' and ItemCode = ").append(columnName);
			sql.append("), ifnull(").append(columnName).append(",'')) ").append(columnName);
		}
		return sql;
	}

	@Override
	public void loadReportData(List datas) throws Exception{
		super.loadReportData(datas);
		btnChoolAllRecord_ActionPerformed(null);
	}

	private void btnAppraiseReportActionPerformed(ActionEvent evt) throws Exception {
		viewJasperReport(evt, "StudentAppraise4One.jasper");
	}

	@Override
	protected void actionPrint_ActionPerformed(ActionEvent evt) throws Exception {
		StudentAppraiseFilterPanel filterPanel = (StudentAppraiseFilterPanel) getFilterBasePanel();
		if (filterPanel.isTableFormReportPrint()) {
			super.actionPrint_ActionPerformed(evt);
		} else {
			viewJasperReport(evt, "StudentAppraise4Multi.jasper");
		}
	}

	private void viewJasperReport(ActionEvent evt, String jasperFilePath) throws JRException, SQLException {
		if (jasperReportService == null) {
			jasperReportService = AppContext.getBean("jasperReportService", IJasperReportService.class);
		}
		int[] selectedRows = tblMain.getSelectedRows();
		if (selectedRows == null || selectedRows.length == 0) {
			JOptionPane.showMessageDialog(this, "请先选中要查看的行！！！");
			return;
		}
		Set<String> idSet = new HashSet<String>();
		int idIndex = rptParamInfo.getColumnNames().length - 1;
		for (int i = 0; i < selectedRows.length; i++) {
			idSet.add(String.valueOf(tblMain.getModel().getValueAt(selectedRows[i], idIndex)));
			logger.info("select id:" + String.valueOf(tblMain.getModel().getValueAt(selectedRows[i], idIndex)));
		}

		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("p_ids", idSet);
		JasperPrint print = jasperReportService.getJasperPrint(jasperFilePath, params);
		JasperViewer viewer = new JasperViewer(print, false);
		viewer.setVisible(true);
	}

	protected void btnChoolAllRecord_ActionPerformed(ActionEvent evt) throws Exception {
		int totalRow = tblMain.getModel().getRowCount();
		if (totalRow != 0) {
			tblMain.setRowSelectionInterval(0, totalRow - 1);
		}
	}

	private void initComponents() {
		btnAppraiseReport = new javax.swing.JButton();
		btnAppraiseReport.setText("报告书");
		btnAppraiseReport.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					btnAppraiseReportActionPerformed(evt);
				} catch (Exception e) {
					UIHandler.handleException(StudentAppraiseRptPanel.this, logger, e);
				}
			}
		});
		toolbarPanel.add(btnAppraiseReport, 3);

		btnChoolAllRecord = new javax.swing.JButton();
		btnChoolAllRecord.setText("全选");
		btnChoolAllRecord.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					btnChoolAllRecord_ActionPerformed(evt);
				} catch (Exception e) {
					UIHandler.handleException(StudentAppraiseRptPanel.this, logger, e);
				}
			}
		});
		toolbarPanel.add(btnChoolAllRecord, 4);
	}

	protected javax.swing.JButton btnAppraiseReport;
	protected javax.swing.JButton btnChoolAllRecord;

	public String getWholeSQL() {
		StringBuffer sql = new StringBuffer();
		sql.append("select CHECKID, TJND, Term");
		processNotNullSQL(sql, "BH");
		processNotNullSQL(sql, "SCHOOLBH");
		processNotNullSQL(sql, "GRADEBH");
		processNotNullSQL(sql, "CLASSBH");
		processNotNullSQL(sql, "RXSJ");
		processNotNullSQL(sql, "XH");
		processNotNullSQL(sql, "SCHOOL_NAME");
		processNotNullSQL(sql, "GRADE_NAME");
		processNotNullSQL(sql, "CLASS_NAME");
		processNotNullSQL(sql, "XM");
		processNotNullSQL(sql, "XB");
		processNotNullSQL(sql, "CSRQ");
		processNotNullSQL(sql, "MZ");
		processNotNullSQL(sql, "ZZMM");
		processNotNullSQL(sql, "SY");
		processNotNullSQL(sql, "TJRQ");
		processNotNullSQL(sql, "NL");
		processNotNullSQL(sql, "MARK");
		processNotNullSQL(sql, "SG");
		processNotNullSQL(sql, "SGDJQ");
		processNotNullSQL(sql, "SGDJD");
		processItemResultSQL(sql, "SGBJ");
		processNotNullSQL(sql, "TZ");
		processNotNullSQL(sql, "TZDJQ");
		processNotNullSQL(sql, "TZDJD");
		processItemResultSQL(sql, "TZBJ");
		processNotNullSQL(sql, "YYPJ");
		processNotNullSQL(sql, "YYPJX");
		processNotNullSQL(sql, "TXPJ");
		processNotNullSQL(sql, "FYPJ");
		processNotNullSQL(sql, "XW");
		processNotNullSQL(sql, "XWDJQ");
		processNotNullSQL(sql, "XWDJD");
		processItemResultSQL(sql, "XWBJ");
		processNotNullSQL(sql, "FHL");
		processNotNullSQL(sql, "FHLDJQ");
		processNotNullSQL(sql, "FHLDJD");
		processItemResultSQL(sql, "FHLBJ");
		processNotNullSQL(sql, "WL");
		processNotNullSQL(sql, "WLDJQ");
		processNotNullSQL(sql, "WLDJD");
		processItemResultSQL(sql, "WLBJ");
		processNotNullSQL(sql, "MB");
		processItemResultSQL(sql, "MBBJ");
		processNotNullSQL(sql, "SSY");
		processItemResultSQL(sql, "SSYBJ");
		processNotNullSQL(sql, "SZY");
		processItemResultSQL(sql, "SZYBJ");
		processNotNullSQL(sql, "LSL");
		processNotNullSQL(sql, "RSL");
		processNotNullSQL(sql, "QTYB");
		processItemResultSQL(sql, "LQG");
		processItemResultSQL(sql, "RQG");
		processItemResultSQL(sql, "LSY");
		processItemResultSQL(sql, "RSY");
		processItemResultSQL(sql, "BS");
		processItemResultSQL(sql, "TL");
		processItemResultSQL(sql, "XJ");
		processItemResultSQL(sql, "BB");
		processNotNullSQL(sql, "QSBNUM");
		processNotNullSQL(sql, "RQB");
		processNotNullSQL(sql, "RQS");
		processNotNullSQL(sql, "RQH");
		processNotNullSQL(sql, "HQB");
		processNotNullSQL(sql, "HQS");
		processNotNullSQL(sql, "HQH");
		processItemResultSQL(sql, "YZB");
		processItemResultSQL(sql, "BTT");
		processItemResultSQL(sql, "XK");
		processItemResultSQL(sql, "JZ");
		processItemResultSQL(sql, "SZ");
		processItemResultSQL(sql, "PZ");
		processItemResultSQL(sql, "XZ");
		processItemResultSQL(sql, "GP");
		processItemResultSQL(sql, "FEI");
		processItemResultSQL(sql, "XT");
		processNotNullSQL(sql, "XHDB");
		processItemResultSQL(sql, "FHCL");
		processItemResultSQL(sql, "PFB");
		processNotNullSQL(sql, "ZG");
		processNotNullSQL(sql, "ZGDJQ");
		processNotNullSQL(sql, "ZGDJD");
		processItemResultSQL(sql, "ZGBJ");
		processNotNullSQL(sql, "JK");
		processNotNullSQL(sql, "JKDJQ");
		processNotNullSQL(sql, "JKDJD");
		processItemResultSQL(sql, "JKBJ");
		processNotNullSQL(sql, "GPK");
		processNotNullSQL(sql, "GPKDJQ");
		processNotNullSQL(sql, "GPKDJD");
		processItemResultSQL(sql, "GPKBJ");
		processNotNullSQL(sql, "BJL");
		processNotNullSQL(sql, "GSTJ");
		processNotNullSQL(sql, "JJX");
		processItemResultSQL(sql, "BMKY");
		processItemResultSQL(sql, "SJSR");
		processNotNullSQL(sql, "WSM");
		processNotNullSQL(sql, "WSMDJQ");
		processNotNullSQL(sql, "WSMDJD");
		processItemResultSQL(sql, "WSMBJ");
		processNotNullSQL(sql, "LDTY");
		processNotNullSQL(sql, "LDTYDJQ");
		processNotNullSQL(sql, "LDTYDJD");
		processItemResultSQL(sql, "LDTYBJ");
		processNotNullSQL(sql, "LL");
		processNotNullSQL(sql, "LLDJQ");
		processNotNullSQL(sql, "LLDJD");
		processItemResultSQL(sql, "LLBJ");
		processNotNullSQL(sql, "NNL");
		processNotNullSQL(sql, "NNLDJQ");
		processNotNullSQL(sql, "NNLDJD");
		processItemResultSQL(sql, "NNLBJ");
		processNotNullSQL(sql, "ZWT");
		processNotNullSQL(sql, "ZWTDJQ");
		processNotNullSQL(sql, "ZWTDJD");
		processItemResultSQL(sql, "ZWTBJ");
		processNotNullSQL(sql, "T1Name");
		processNotNullSQL(sql, "T1");
		processNotNullSQL(sql, "T2name");
		processNotNullSQL(sql, "T2");
		processNotNullSQL(sql, "T3Name");
		processNotNullSQL(sql, "T3");
		processNotNullSQL(sql, "T4Name");
		processNotNullSQL(sql, "T4");
		processNotNullSQL(sql, "T5Name");
		processNotNullSQL(sql, "T5");
		processNotNullSQL(sql, "T6Name");
		processNotNullSQL(sql, "T6");
		processNotNullSQL(sql, "T7Name");
		processNotNullSQL(sql, "T7");
		processNotNullSQL(sql, "T8Name");
		processNotNullSQL(sql, "T8");
		processNotNullSQL(sql, "T9Name");
		processNotNullSQL(sql, "T9");
		processNotNullSQL(sql, "T10Name");
		processNotNullSQL(sql, "T10");
		processItemResultSQL(sql, "SLBH");
		processNotNullSQL(sql, "MBPJ");
		processNotNullSQL(sql, "SSYPJ");
		processNotNullSQL(sql, "SZYPJ");
		processNotNullSQL(sql, "PXPJ");
		processNotNullSQL(sql, "SLPJ");
		processNotNullSQL(sql, "XJH");
		processNotNullSQL(sql, "YYPJB");
		processNotNullSQL(sql, "TJSY");
		processNotNullSQL(sql, "MB1");
		processNotNullSQL(sql, "MB2");
		processNotNullSQL(sql, "MB3");
		processNotNullSQL(sql, "TJFS");
		processNotNullSQL(sql, "TJPJ");
		processNotNullSQL(sql, "YYPJH");
		processNotNullSQL(sql, "XMPY");
		processNotNullSQL(sql, "HXP");
		processNotNullSQL(sql, "HXPPJ");
		processItemResultSQL(sql, "EB");
		processItemResultSQL(sql, "YB");
		processItemResultSQL(sql, "TB");
		processItemResultSQL(sql, "JB");
		processItemResultSQL(sql, "PF");
		processItemResultSQL(sql, "JZX");
		processItemResultSQL(sql, "LBJ");
		processItemResultSQL(sql, "JHJS");
		processNotNullSQL(sql, "GBZAM");
		processNotNullSQL(sql, "DHSA");
		processNotNullSQL(sql, "DHSZ");
		processItemResultSQL(sql, "GG");
		processItemResultSQL(sql, "BMKT");
		processItemResultSQL(sql, "YGHKT");
		processItemResultSQL(sql, "YGEKY");
		processItemResultSQL(sql, "YGEKT");
		processNotNullSQL(sql, "BLOOD");
		processNotNullSQL(sql, "YW");
		processNotNullSQL(sql, "TW");
		processNotNullSQL(sql, "CXBH");
		processNotNullSQL(sql, "LJSL");
		processNotNullSQL(sql, "RJSL");
		processItemResultSQL(sql, "JMY");
		processItemResultSQL(sql, "WGFB");
		processItemResultSQL(sql, "YYB");
		processItemResultSQL(sql, "PEI");
		processNotNullSQL(sql, "LSLS");
		processNotNullSQL(sql, "RSLS");
		processNotNullSQL(sql, "LJSLS");
		processNotNullSQL(sql, "RJSLS");
		processItemResultSQL(sql, "RTL");
		processItemResultSQL(sql, "WSZQ");
		processNotNullSQL(sql, "RS");
		processNotNullSQL(sql, "BXP");
		processNotNullSQL(sql, "XXB");
		processNotNullSQL(sql, "SFZH");
		processNotNullSQL(sql, "DH");
		processNotNullSQL(sql, "ADDRESS");
		processNotNullSQL(sql, "RXYFJZS");
		processNotNullSQL(sql, "YMA");
		processNotNullSQL(sql, "YMAJY");
		processNotNullSQL(sql, "YMB");
		processNotNullSQL(sql, "YMBJY");
		processNotNullSQL(sql, "YMC");
		processNotNullSQL(sql, "YMCJY");
		processNotNullSQL(sql, "YMD");
		processNotNullSQL(sql, "YMDJY");
		processNotNullSQL(sql, "YME");
		processNotNullSQL(sql, "YMEJY");
		processNotNullSQL(sql, "YMQT");
		processNotNullSQL(sql, "RXHJZS");
		processNotNullSQL(sql, "JWBS");
		processNotNullSQL(sql, "QCQFY");
		processNotNullSQL(sql, "STUDGUID");
		processNotNullSQL(sql, "SCHOOLID");
		processNotNullSQL(sql, "STUDNUM");
		processNotNullSQL(sql, "YMF");
		processNotNullSQL(sql, "YMFJY");
		processNotNullSQL(sql, "V1Name");
		processNotNullSQL(sql, "V1");
		processNotNullSQL(sql, "V2Name");
		processNotNullSQL(sql, "V2");
		processNotNullSQL(sql, "V3Name");
		processNotNullSQL(sql, "V3");
		processNotNullSQL(sql, "V4Name");
		processNotNullSQL(sql, "V4");
		processNotNullSQL(sql, "V5Name");
		processNotNullSQL(sql, "V5");
		return sql.toString();
	}
}